// Robert van Seggelen, robertvs@gmail.com
// 2011
jQuery.fn.mp3player = function(userOptions) {
	//default options
	var options = {
		player: null, //the html audio element ref
		$player: null, //the jQuery html audio element ref
		$domElement: $(this), //the domelement in which to put the audio element
		mp3Url: null, //the path of the mp3 file to play
		playerOptions: { //container holding options for the player
			height: $(this).height(),
			width: $(this).width(),
			autoPlay: false,
			showControls: false,
			volume: 0.8 //volume must be between 0 and 1
		},
		flash: { //fallback flash if browser can't play mp3
			player: 'niftyPlayer.swf',
			js: 'niftyPlayer.js' //maybe put this code in this code here?
		},
		//events
		onPlayerReady: function(){}, //the player has been initialized
		onTrackEnded: function(){}, //when the track has played through and has stopped
		onTrackPlaying: function(){}, //when the track startplaying from stoped state and paused state
		onTrackStopped: function(){}, //when the track has been stopped
		onTrackPaused: function(){}, //when the track is paused
		onTrackLoading: function(){}, //when the track is loading
		onTrackLoaded: function(){}, //when the track has finished loading completely
		onInit: function(){}, //when everything is initialized
		onError: function(msg){} //if osmething goes wrong
	}; 
	//merge the default options with the user defined ones
	$.extend(true, options, userOptions);
	
	//set up player
	$(function(){
		options.$player = $('<audio />').appendTo(options.$domElement);
		options.player = options.$player[0];
		if(options.playerOptions.showControls)
			options.$player.attr('controls', 'controls');
		//options.$player.attr('preload', 'auto');
		options.player.volume = options.playerOptions.volume;
		options.onInit();
		if(options.trackUrl)
			PlayTrack(options.mp3Url);
	});
	function PlayTrack(mp3url){
		options.mp3Url = mp3url;
		options.$player.attr('src', mp3url);
		options.$player.bind('ended', options.onTrackEnded);
		//options.player.bind('canplay', options.onPlayerReady);
		options.$player.bind('playing', options.onTrackPlaying);
		options.$player.bind('paused', options.onTrackPaused);
		options.$player.bind('error', options.onError);
		options.$player.bind('loadedmetadata', options.onTrackLoading);
		options.$player.bind('onloadeddata', options.onTrackLoaded);
		options.$player.bind('canplay', function(){
			options.onPlayerReady;
			//options.player.stop();
			//if(options.playerOptions.autoPlay)
			options.player.play();
		});
	}
	//will play the current loaded video
	this.Play = function(){
		if(options.player)
			options.player.play();
		else
			options.onError("player not initialized");
	}
	//will stop all current playback
	this.Stop = function(){
		if(options.player){
			options.$player.attr('src', null);
			options.player.pause();
			if(options.player.currentTime && options.player.currentTime > 0)
				options.player.currentTime = 0;
			options.onTrackStopped;
		}
		else{
			options.onError("player not initialized");
		}
		//alert('stop');
	}
	//will pause current playback
	this.Pause = function(){
		if(options.player)
			options.player.pause();
		else
			options.onError("player not initialized");
	}
	this.PlayUrl = function(mp3Url, autoPlay){
		options.playerOptions.autoPlay = autoPlay;
		if(options.player.paused && options.player.currentTime > 0)
			options.player.play();
		else
			PlayTrack(mp3Url);
	}
	this.SetVolume = function(volume){
		if(options.player)
			options.player.volume = volume;
	}
	return this;
};